from flask import request
from flask_shop.role import role_api, role_bp
from flask_shop import models, db
from flask_restful import Resource
from flask_shop.utils.message import to_dict_msg


class RoleView(Resource):
    def get(self):
        try:
            roles = models.Role.query.all()
            role_list = [r.to_dict() for r in roles]
            return to_dict_msg(200, role_list, '获取角色列表成功！')
        except Exception as e:
            print(e)
            return to_dict_msg(20000)

    def post(self):
        name = request.form.get('name')
        desc = request.form.get('desc')
        try:
            if name:
                role = models.Role(name=name, desc=desc)
                db.session.add(role)
                db.session.commit()
                return to_dict_msg(200, msg='增加角色成功')
            return to_dict_msg(10000)
        except Exception as e:
            print(e)
            return to_dict_msg(20000)

    def put(self):
        try:
            id =  int(request.form.get('id'))
            name = request.form.get('name') if request.form.get('name') else ''
            desc = request.form.get('desc') if request.form.get('desc') else ''
            role = models.Role.query.get(id)
            if role:
                role.name = name
                role.desc = desc
                db.session.commit()
                return to_dict_msg(200, msg='修改角色信息成功')
            return to_dict_msg(10020)
        except Exception as e:
            print(e)
            return to_dict_msg(20000)

    def delete(self):
        try:
            id = int(request.form.get('id'))
            role = models.Role.query.get(id)
            if role:
                db.session.delete(role)
                db.session.commit()
                return to_dict_msg(200, msg='删除角色成功')
            return to_dict_msg(200, msg='角色不存在')
        except Exception as e:
            print(e)
            return to_dict_msg(20000)


@role_bp.route('/del_menu/<int:rid>/<int:menu_id>')
def del_menu(rid, menu_id):
    try:
        role = models.Role.query.get(rid)
        menu = models.Menu.query.get(menu_id)
        if all([role, menu]):
            if menu in role.menus:
                role.menus.remove(menu)
                if menu.level == 1:
                    for temp_menu in menu.children:
                        if temp_menu in role.menus:
                            role.menus.remove(temp_menu)
                db.session.commit()
                return to_dict_msg(200, data=role.get_menu_dict(), msg='删除权限成功')
            return to_dict_msg(10021)
        return to_dict_msg(10000)
    except Exception as e:
        print(e)
        return to_dict_msg(20000)


@role_bp.route('/set_menu/<int:rid>', methods=['POST'])
def set_menu(rid):
    try:
        role = models.Role.query.get(rid)
        mids = request.form.get('mids')
        if role:
            role.menus = []
            for mid in mids.split(','):
                if mid:
                    temp_menu = models.Menu.query.get(mid)
                    if temp_menu:
                        role.menus.append(temp_menu)
            db.session.commit()
            return to_dict_msg(200, '分配权限成功！')
        return to_dict_msg(10020)
    except Exception as e:
        print(e)
        return to_dict_msg(20000)


role_api.add_resource(RoleView, '/role')
